﻿using System;
using WorkOrder.Model;
using System.Collections;

namespace WorkOrder.Business
{
    public class Business_Table_Changeover
    {
        DbHelper dbhelper = new DbHelper();

        #region 保存
        public bool Insert(Model_Table_Changeover model)
        {
            string sqlstr = " insert into Table_Changeover (area_id,changeover_id,changeover_number,customer_address,customer_name,"+
            "customer_tell,debug_unit_id,equipment_model,fact_id,first_cute_time,inte_operator,"+
            " inte_unit_id,product_id,station_name,supe_operator,supe_unit_id,town_id,user_id,changeover_is_succeed,is_problem,order_number,debug_operator) " +
            "values (";
            sqlstr += model.Area_id + ",'" + model.Changeover_id + "','" + model.Changeover_number + "','" + model.Customer_address + "','" + model.Customer_name + "','" + model.Customer_tell +
                "'," + model.Debug_unit_id + ",'" + model.Equipment_model + "'," + model.Factory_id + ",'" + model.First_changeover_time + "','" + model.Integrated_operator +
                "'," + model.Integrated_unit_id + "," + model.Product_id + ",'" + model.Station_name + "','" + model.Supervision_operator + "'," + model.Supervision_unit_id + "," +
                model.Town_id + "," + model.UserID + ",0,0,'" + model.Order_number + "','" + model.Debug_operator + "')";

            if (dbhelper.ExecuteNoQuery(sqlstr) > 0)
                return true;
            return false;
        }
        #endregion

        #region 更新信息
        public bool UpdateInfo(Model_Table_Changeover model)
        {
            string sql = "update Table_Changeover set area_id=" + model.Area_id + ",customer_address='" + model.Customer_address + "',customer_name='" + model.Customer_name
                + "',customer_tell='" + model.Customer_tell + "',debug_unit_id=" + model.Debug_unit_id + ",equipment_model='" + model.Equipment_model + "',fact_id=" + model.Factory_id
                + ",first_cute_time='" + model.First_changeover_time + "',inte_operator='" + model.Integrated_operator + "',inte_unit_id=" + model.Integrated_unit_id + ",product_id=" + model.Product_id
                + ",station_name='" + model.Station_name + "',supe_operator='" + model.Supervision_operator + "',supe_unit_id=" + model.Supervision_unit_id + ",town_id=" + model.Town_id
                + ",try_time=" + (!string.IsNullOrEmpty(model.Try_time ) ? "'" + model.Try_time + "'" : "NULL")
                + ",app_main_time=" + (!string.IsNullOrEmpty(model.App_main_time) ? "'" + model.App_main_time + "'" : "NULL")
                + ",con_main_time=" + (!string.IsNullOrEmpty(model.Con_main_time) ? "'" + model.Con_main_time + "'" : "NULL")
                + " where changeover_id='" + model.Changeover_id + "'";
            if (dbhelper.ExecuteNoQuery(sql) > 0)
                return true;
            return false;
        }
        #endregion

        #region 更新软调工作表提交时间

        /// <summary>
        /// 更新软调工作表提交时间
        /// </summary>
        /// <param name="changeover_id">ID</param>
        /// <param name="debugging_confirm_time">软调工作表最终确认时间</param>
        /// <param name="debugging_error_times">软调工作表错误次数</param>
        /// <param name="debug_operator">软调人员</param>
        /// <returns></returns>
        public bool UpdateDebugging(string changeover_id, string debugging_confirm_time, string debugging_error_times, string debug_operator)
        {
            string sql;
            if (debugging_confirm_time == "" || debugging_confirm_time.ToUpper() == "NULL")
                sql = "declare @return_value int; exec @return_value=UP_UpdateDebugging '" + changeover_id + "',NULL,'" + debugging_error_times + "','" + @debug_operator + "'; select 'return_value'=@return_value;";
            else
                sql = "declare @return_value int; exec @return_value=UP_UpdateDebugging '" + changeover_id + "','" + debugging_confirm_time + "','" + debugging_error_times + "','" + @debug_operator + "'; select 'return_value'=@return_value;";
            if (Convert.ToInt32(dbhelper.ExecuteScalar(sql)) == 0)
                return true;
            return false;
        }
        #endregion

        #region 更新拨测表提交时间
        /// <summary>
        /// 更新拨测表提交时间
        /// </summary>
        /// <param name="changeover_id">ID</param>
        /// <param name="test_confirm_time">拨测表最后提交时间</param>
        /// <param name="test_error_times">拨测表错误次数</param>
        /// <returns></returns>
        public bool UpdateTest(string changeover_id, string test_confirm_time, string test_error_times)
        {
            string sql;
            if (test_confirm_time == "" || test_confirm_time.ToUpper() == "NULL")
                sql = "declare @return_value int; exec @return_value=UP_UpdateTest '" + changeover_id + "',NULL" + ",'" + test_error_times + "'; select 'return_value'=@return_value;";
            else
                sql = "declare @return_value int; exec @return_value=UP_UpdateTest '" + changeover_id + "','" + test_confirm_time + "','" + test_error_times + "'; select 'return_value'=@return_value;";
            if (Convert.ToInt32(dbhelper.ExecuteScalar(sql)) == 0)
                return true;
            return false;
        }
        #endregion

        #region 更新最终回访时间
        public bool UpdateReturnVisit(string changeover_id, string return_visit_time)
        {
            string sql;
            if (return_visit_time == "" || return_visit_time.ToUpper() == "NULL")
                sql = "declare @return_value int; exec @return_value=UP_UpdateReturnVisit '" + changeover_id + "',NULL; select 'return_value'=@return_value;";
            else
                sql = "declare @return_value int; exec @return_value=UP_UpdateReturnVisit '" + changeover_id + "','" + return_visit_time + "'; select 'return_value'=@return_value;";
            if (Convert.ToInt32(dbhelper.ExecuteScalar(sql)) == 0)
                return true;
            return false;
        }
        #endregion

        #region 更新客户交付时间
        public bool UpdateDelivery(string changeover_id, string delivery_time)
        {
            string sql;
            if (delivery_time == "" || delivery_time.ToUpper() == "NULL")
                sql = "declare @return_value int; exec @return_value=UP_UpdateDelivery '" + changeover_id + "',NULL; select 'return_value'=@return_value;";
            else
                sql = "declare @return_value int; exec @return_value=UP_UpdateDelivery '" + changeover_id + "','" + delivery_time + "'; select 'return_value'=@return_value;";
            if (Convert.ToInt32(dbhelper.ExecuteScalar(sql)) == 0)
                return true;
            return false;
        }
        #endregion

        #region 更新状态
        public bool UpdateStatus(string changeover_id, string changeover_status_id)
        {
            string sql = "update Table_Changeover set status=" + changeover_status_id + " where changeover_id='" + changeover_id + "'";
            if (dbhelper.ExecuteNoQuery(sql) > 0)
                return true;
            return false;
        }
        #endregion

        #region 检查业务单是否存在
        /// <summary>
        /// 检查业务单是否存在
        /// </summary>
        /// <returns>true：，false：</returns>
        public bool HasExist(string orderNumber)
        {
            string sql = "select top 1 changeover_id from Table_Changeover where order_number='" + orderNumber + "'";
            return dbhelper.ExecuteScalar(sql) != null;
        }
        #endregion

        #region 得到单个数据集
        public Model_Table_Changeover GetData(string keyValue)
        {
            string sql = "select A.*,B.fact_name,C.inte_unit_name,D.debug_unit_name,E.supe_unit_name,F.changeover_status from Table_Changeover A,Table_Factory B,Table_Integrated_unit C,Table_Debugging_unit D,Table_Supervision_unit E,Table_Changeover_Status F where " +
                "A.changeover_id='" + keyValue + "' and A.fact_id=B.fact_id and A.inte_unit_id=C.inte_unit_id and A.debug_unit_id=D.debug_unit_id and A.supe_unit_id=E.supe_unit_id and A.status=F.changeover_status_id";
            using (System.Data.SqlClient.SqlDataReader rdr = dbhelper.Search(sql))
            {
                if (rdr != null && rdr.Read())
                {
                    Model_Table_Changeover tmp;
                    tmp = new Model_Table_Changeover();
                    tmp.Area_id = rdr["area_id"].ToString();
                    tmp.Changeover_id = rdr["changeover_id"].ToString();
                    if (rdr["changeover_is_succeed"] != null && rdr["changeover_is_succeed"].ToString() != "0")
                        tmp.Changeover_is_succeed = "是";
                    else
                        tmp.Changeover_is_succeed = "否";
                    tmp.Changeover_number = rdr["changeover_number"].ToString();
                    tmp.Customer_address = rdr["customer_address"].ToString();
                    tmp.Customer_name = rdr["customer_name"].ToString();
                    tmp.Customer_tell = rdr["customer_tell"].ToString();
                    if (rdr["status"] != null)
                        tmp.Status_id = rdr["status"].ToString();
                    else
                        tmp.Status_id = "";
                    if (rdr["changeover_status"] != null)
                        tmp.Station_name = rdr["changeover_status"].ToString();
                    else
                        tmp.Station_name = "";
                    if (rdr["debug_operator"] != null)
                        tmp.Debug_operator = rdr["debug_operator"].ToString();
                    else
                        tmp.Debug_operator = "";
                    tmp.Debug_unit_id = rdr["debug_unit_id"].ToString();
                    if (rdr["debugging_confirm_time"] != null)
                        tmp.Debugging_confirm_time = rdr["debugging_confirm_time"].ToString();
                    else
                        tmp.Debugging_confirm_time = "";
                    if (rdr["debugging_confirm_timeout"] != null)
                        tmp.Debugging_confirm_timeout = rdr["debugging_confirm_timeout"].ToString();
                    else
                        tmp.Debugging_confirm_timeout = "";
                    if (rdr["debugging_error_times"] != null)
                        tmp.Debugging_error_times = rdr["debugging_error_times"].ToString();
                    else
                        tmp.Debugging_error_times = "";
                    if (rdr["delivery_time"] != null)
                        tmp.Delivery_time = rdr["delivery_time"].ToString();
                    else
                        tmp.Delivery_time = "";
                    if (rdr["delivery_timeout"] != null)
                        tmp.Delivery_timeout = rdr["delivery_timeout"].ToString();
                    else
                        tmp.Delivery_timeout = "";
                    tmp.Equipment_model = rdr["equipment_model"].ToString();
                    tmp.Order_number = rdr["order_number"].ToString();
                    tmp.Factory_id = rdr["fact_id"].ToString();
                    tmp.First_changeover_time = rdr["first_cute_time"].ToString();
                    tmp.Integrated_operator = rdr["inte_operator"].ToString();
                    tmp.Integrated_unit_id = rdr["inte_unit_id"].ToString();
                    tmp.Product_id = rdr["product_id"].ToString();
                    if (rdr["return_visit_time"] != null)
                        tmp.Return_visit_time = rdr["return_visit_time"].ToString();
                    else
                        tmp.Return_visit_time = "";
                    if (rdr["try_time"] != null)
                        tmp.Try_time = rdr["try_time"].ToString();
                    else
                        tmp.Try_time = "";
                    if (rdr["con_main_time"] != null)
                        tmp.Con_main_time = rdr["con_main_time"].ToString();
                    else
                        tmp.Con_main_time = "";
                    if (rdr["app_main_time"] != null)
                        tmp.App_main_time = rdr["app_main_time"].ToString();
                    else
                        tmp.App_main_time = "";
                    if (rdr["return_visit_timeout"] != null)
                        tmp.Return_visit_timeout = rdr["return_visit_timeout"].ToString();
                    else
                        tmp.Return_visit_timeout = "";
                    tmp.Station_name = rdr["station_name"].ToString();
                    tmp.Supervision_operator = rdr["supe_operator"].ToString();
                    tmp.Supervision_unit_id = rdr["supe_unit_id"].ToString();
                    if (rdr["test_confirm_time"] != null)
                        tmp.Test_confirm_time = rdr["test_confirm_time"].ToString();
                    else
                        tmp.Test_confirm_time = "";
                    if (rdr["test_confirm_timeout"] != null)
                        tmp.test_confirm_timeout = rdr["test_confirm_timeout"].ToString();
                    else
                        tmp.test_confirm_timeout = "";
                    if (rdr["test_error_times"] != null)
                        tmp.Test_error_times = rdr["test_error_times"].ToString();
                    else
                        tmp.Test_error_times = "";
                    tmp.Town_id = rdr["town_id"].ToString();
                    tmp.Factory_name = rdr["fact_name"].ToString();
                    tmp.Integrated_unit_name = rdr["inte_unit_name"].ToString();
                    tmp.Debugging_unit_name = rdr["debug_unit_name"].ToString();
                    tmp.Supervision_unit_name = rdr["supe_unit_name"].ToString();
                    rdr.Close();
                    dbhelper.CloseDbConnection();
                    return tmp;
                }
                else return null;
            }
        }
        #endregion

        #region 根据条件得到数据集
        public System.Data.DataSet GetDatas(string condition, int pageSize, int currentPage, string orderField, OrderType ot, string userID)
        {
            string sql = "UP_GET_PAGEINFO 'Table_Changeover','*"
                     + "','" + condition + "','Table_User$remark as name_remark$A.user_id=B.user_id|" +
                     "Table_Product$product_name$A.product_id=B.product_id|" +
                     "(select M.town_id,M.town_name,N.area_name from Table_Town as M,Table_Area N where M.area_id=N.area_id)$area_name,town_name$A.town_id=B.town_id|" +
                     "Table_Factory$fact_name$A.fact_id=B.fact_id|" +
                     "Table_Integrated_unit$inte_unit_name$A.inte_unit_id=B.inte_unit_id|" +
                     "Table_Debugging_unit$debug_unit_name$A.debug_unit_id=B.debug_unit_id|" +
                     "Table_Changeover_Status$changeover_status$A.status=B.changeover_status_id|" +
                     "Table_Supervision_unit$supe_unit_name$A.supe_unit_id=B.supe_unit_id" +
                     "','" + orderField + "'," + (ot == OrderType.ASC ? 0 : 1) + "," + pageSize + "," + currentPage;
            return dbhelper.Query(sql);
        }

        public System.Data.DataTable GetDatas(string sqlstr)
        {
            return dbhelper.Query("changeovertable", sqlstr);
        }

        public System.Data.DataTable QueryDatas(string condition)
        {
            string sqlstr = "SELECT A.*,B.supe_unit_name FROM (" +
                "SELECT A.*,B.changeover_status FROM (" +
                "SELECT A.*,B.debug_unit_name FROM (" +
                "SELECT A.*,B.inte_unit_name FROM (" +
                "SELECT A.*,B.fact_name FROM (" +
                "SELECT A.*,B.area_name,town_name FROM (" +
                "SELECT A.*,B.product_name FROM (" +
                "SELECT A.*,B.remark as name_remark FROM ( " +
                "select * from Table_Changeover where " + condition + ") A " +
                "LEFT JOIN Table_User B ON A.user_id=B.user_id) A " +
                "LEFT JOIN Table_Product B ON A.product_id=B.product_id) A " +
                "LEFT JOIN (select M.town_id,M.town_name,N.area_name from Table_Town as M,Table_Area N where M.area_id=N.area_id) B ON A.town_id=B.town_id) A " +
                "LEFT JOIN Table_Factory B ON A.fact_id=B.fact_id) A " +
                "LEFT JOIN Table_Integrated_unit B ON A.inte_unit_id=B.inte_unit_id) A " +
                "LEFT JOIN Table_Debugging_unit B ON A.debug_unit_id=B.debug_unit_id) A " +
                "LEFT JOIN Table_Changeover_Status B ON A.status=B.changeover_status_id) A " +
                "LEFT JOIN Table_Supervision_unit B ON A.supe_unit_id=B.supe_unit_id " +
                "order by first_cute_time desc ";
            return dbhelper.Query("changeovertable", sqlstr);
        }
        #endregion

        public System.Data.DataTable QueryChangeoverTimeout(string changeover_condition, string timeout_condition)
        {
            string sqlstr = "EXEC UP_GetChangeoverTimeout 'AND " + changeover_condition + "','AND " + timeout_condition + "'";
            return dbhelper.Query("", sqlstr);
        }

        #region 查询割接单问题
        //public System.Data.DataSet GetChangeoverExceptions(string condition, int pageSize, int currentPage, string orderField, OrderType ot, string userID)
        //{
        //    string sql = "UP_GET_PAGEINFO 'Table_Changeover','*"
        //             + "','" + condition + "','(select problem_strat_time,cute_problem_desc,changeover_problems_type_name from Table_Cut_Problems,Table_Changeover_problems_type where Table_Cut_Problems.cute_problem_type_id=Table_Changeover_problems_type.changeover_problems_type_id)$changeover_problems_type_name,problem_strat_time,left(cute_problem_desc,25) as cute_problem_desc$B.changeover_id=A.changeover_id|" +
        //             "Table_Product$product_name$A.product_id=B.product_id|" +
        //             "Table_Area$area_name$A.area_id=B.area_id|" +
        //             "Table_Town$town_name$A.town_id=B.town_id|" +
        //             "Table_Factory$fact_name$A.fact_id=B.fact_id|" +
        //             "Table_Integrated_unit$inte_unit_name$A.inte_unit_id=B.inte_unit_id|" +
        //             "Table_Debugging_unit$debug_unit_name$A.debug_unit_id=B.debug_unit_id|" +
        //             "Table_Changeover_Status$changeover_status$A.status=B.changeover_status_id|" +
        //             "Table_Supervision_unit$supe_unit_name$A.supe_unit_id=B.supe_unit_id" +
        //             "','" + orderField + "'," + (ot == OrderType.ASC ? 0 : 1) + "," + pageSize + "," + currentPage;
        //    return new DbHelper().Query(sql);
        //}
        public System.Data.DataTable GetChangeoverExceptions(string sqlstr)
        {
            sqlstr = "select B.changeover_id,B.order_number,C.area_name,D.town_name,E.product_name,B.station_name,K.changeover_status,F.changeover_problems_type_name," +
                "A.problem_strat_time,left(A.cute_problem_desc,25)+'......' as cute_problem_desc,G.supe_unit_name,H.inte_unit_name,I.debug_unit_name,J.fact_name,B.first_cute_time,B.equipment_model " +
                "from Table_Cut_Problems A,Table_Changeover B,Table_Area C,Table_Town D,Table_Product E,Table_Changeover_problems_type F," +
                "Table_Supervision_unit G,Table_Integrated_unit H,Table_Debugging_unit I,Table_Factory J,Table_Changeover_Status K " +
                "where B.changeover_id=A.changeover_id and B.area_id=C.area_id and B.town_id=D.town_id and B.product_id=E.product_id " +
                "and A.cute_problem_type_id=F.changeover_problems_type_id and B.supe_unit_id=G.supe_unit_id and B.inte_unit_id=H.inte_unit_id " +
                "and B.debug_unit_id=I.debug_unit_id and B.fact_id=J.fact_id and B.status=K.changeover_status_id and " + sqlstr;
            return dbhelper.Query("changeoveexceptionrtable", sqlstr);
        }
        #endregion

        #region 删除记录
        public bool Delete(string fieldName, string fieldValue, bool isIntOrBool)
        {
            string sqlstr;
            if (isIntOrBool)
                sqlstr = "delete Table_Changeover where " + fieldName + "=" + fieldValue;
            else
                sqlstr = "delete Table_Changeover where " + fieldName + "='" + fieldValue + "'";
            if (dbhelper.ExecuteNoQuery(sqlstr) > 0)
                return true;
            else
                return false;
        }
        #endregion

        #region 返回产品类别资料
        public System.Data.DataSet GetProduct()
        {
            string sqlstr = "select product_id,product_name from Table_Product";
            return dbhelper.Query(sqlstr);
        }
        #endregion

        #region 根据id返回产品类别
        public Model_Table_Product GetProductByID(string id)
        {
            string sqlstr = "select * from Table_Product where product_id=" + id;
            using (System.Data.SqlClient.SqlDataReader rdr = dbhelper.Search(sqlstr))
            {
                if (rdr != null && rdr.Read())
                {
                    Model_Table_Product tmp = new Model_Table_Product(rdr["product_id"].ToString(), rdr["product_name"].ToString(), rdr["remark"].ToString());
                    rdr.Close();
                    dbhelper.CloseDbConnection();
                    return tmp;
                }
                else
                    return null;
            }
        }
        #endregion

        #region 返回镇区资料
        public System.Data.DataSet GetTown()
        {
            string sqlstr = "select town_id,town_name from Table_Town";
            return dbhelper.Query(sqlstr);
        }
        #endregion

        #region 返回区域资料
        public System.Data.DataSet GetArea()
        {
            string sqlstr = "select area_id,area_name from Table_Area";
            return dbhelper.Query(sqlstr);
        }
        #endregion

        #region 根据条件返回区域
        public Model_Table_Area GetArea(string condition)
        {
            if (string.IsNullOrEmpty(condition)) condition = "1=1";
            string sqlstr = "select * from Table_Area where " + condition;
            using (System.Data.SqlClient.SqlDataReader rdr = dbhelper.Search(sqlstr))
            {
                if (rdr != null && rdr.Read())
                {
                    Model_Table_Area tmp = new Model_Table_Area(rdr["area_id"].ToString(), rdr["area_name"].ToString(), rdr["remark"].ToString());
                    rdr.Close();
                    dbhelper.CloseDbConnection();
                    return tmp;
                }
                else
                    return null;
            }
        }
        #endregion

        #region 根据镇区返回区域
        public Model_Table_Area GetAreaByTown(string townid)
        {
            if (!string.IsNullOrEmpty(townid))
            {
                string sqlstr = "select * from Table_Area,Table_Town where Table_Area.area_id=Table_Town.area_id and Table_Town.town_id=" + townid;
                using (System.Data.SqlClient.SqlDataReader rdr = dbhelper.Search(sqlstr))
                {
                    if (rdr != null && rdr.Read())
                    {
                        Model_Table_Area tmp = new Model_Table_Area(rdr["area_id"].ToString(), rdr["area_name"].ToString(), rdr["remark"].ToString());
                        rdr.Close();
                        dbhelper.CloseDbConnection();
                        return tmp;
                    }
                    else
                        return new Model_Table_Area("", "", "");
                }
            }
            else return new Model_Table_Area("", "", "");
        }
        #endregion

        #region 根据区域返回镇区
        public ArrayList GetTownByArea(string areaid)
        {
            if (!string.IsNullOrEmpty(areaid))
            {
                ArrayList arr = new ArrayList();
                string sqlstr = "select town_id,town_name from Table_Town where area_id=" + areaid;
                using (System.Data.SqlClient.SqlDataReader rdr = dbhelper.Search(sqlstr))
                {
                    if (rdr != null)
                    {
                        while (rdr.Read())
                        {
                            object[] tmp = { rdr["town_id"], rdr["town_name"] };
                            arr.Add(tmp);
                        }
                        rdr.Close();
                        dbhelper.CloseDbConnection();
                        return arr;
                    }
                    else
                        return null;
                }
            }
            else return null;
        }
        #endregion

        #region 根据区域返回镇区ID
        public ArrayList GetTownIdByArea(string areaid)
        {
            if (!string.IsNullOrEmpty(areaid))
            {
                ArrayList arr = new ArrayList();
                string sqlstr = "select town_id from Table_Town where area_id=" + areaid;
                using (System.Data.SqlClient.SqlDataReader rdr = dbhelper.Search(sqlstr))
                {
                    if (rdr != null)
                    {
                        while (rdr.Read())
                        {
                            arr.Add(rdr["town_id"]);
                        }
                        rdr.Close();
                        dbhelper.CloseDbConnection();
                        return arr;
                    }
                    else
                        return null;
                }
            }
            else return null;
        }
        #endregion

        #region 根据id返回镇区
        public Model_Table_Town GetTownByID(string id)
        {
            string sqlstr = "select * from Table_Town where town_id=" + id;
            using (System.Data.SqlClient.SqlDataReader rdr = dbhelper.Search(sqlstr))
            {
                if (rdr != null && rdr.Read())
                {
                    Model_Table_Town tmp = new Model_Table_Town(rdr["town_id"].ToString(), rdr["area_id"].ToString(), rdr["town_name"].ToString(), rdr["remark"].ToString());
                    rdr.Close();
                    dbhelper.CloseDbConnection();
                    return tmp;
                }
                else
                    return null;
            }
        }
        #endregion

        #region 返回设备厂家资料
        public System.Data.DataSet GetFactory()
        {
            string sqlstr = "select fact_id,fact_name from Table_Factory";
            return dbhelper.Query(sqlstr);
        }
        #endregion

        #region 返回集成单位资料
        public System.Data.DataSet GetIntegratedUnit()
        {
            string sqlstr = "select inte_unit_id,inte_unit_name from Table_Integrated_unit";
            return dbhelper.Query(sqlstr);
        }
        #endregion

        #region 返回软调单位资料
        public System.Data.DataSet GetDebuggingUnit()
        {
            string sqlstr = "select debug_unit_id,debug_unit_name from Table_Debugging_unit";
            return dbhelper.Query(sqlstr);
        }
        #endregion

        #region 返回监理单位资料
        public System.Data.DataSet GetSupervisionUnit()
        {
            string sqlstr = "select supe_unit_id,supe_unit_name from Table_Supervision_unit";
            return dbhelper.Query(sqlstr);
        }
        #endregion

        #region 返回割接问题类型资料
        public System.Data.DataSet GetChangeoverProblemsType()
        {
            string sqlstr = "select changeover_problems_type_id,changeover_problems_type_name from Table_Changeover_problems_type";
            return dbhelper.Query(sqlstr);
        }
        #endregion

        #region 检查工单是否已存在
        public bool IsExist(string orderNumber)
        {
            string sqlstr = "select top 1 order_number from Table_Changeover where order_number='" + orderNumber + "'";
            return dbhelper.ExecuteScalar(sqlstr) != null;
        }
        #endregion

        #region 返回割接状态资料
        public System.Data.DataSet GetStatus()
        {
            string sqlstr = "select changeover_status_id,changeover_status from Table_Changeover_Status";
            return dbhelper.Query(sqlstr);
        }
        #endregion

        #region 新增割接问题
        public bool AddChangeoverProblems(string changeover_id, string problem_type_id, string problem_desc, string problem_start_time, string problem_end_time)
        {
            string sqlstr;
            if (string.IsNullOrEmpty(problem_end_time))
                sqlstr = "insert into Table_Cut_Problems(changeover_id,cute_problem_desc,cute_problem_id,cute_problem_type_id,problem_strat_time,problem_end_time) values (" +
                    "'" + changeover_id + "','" + problem_desc + "',NEWID()," + problem_type_id + ",'" + problem_start_time + "',NULL)";
            else
                sqlstr = "insert into Table_Cut_Problems(changeover_id,cute_problem_desc,cute_problem_id,cute_problem_type_id,problem_strat_time,problem_end_time) values (" +
                    "'" + changeover_id + "','" + problem_desc + "',NEWID()," + problem_type_id + ",'" + problem_start_time + "','" + problem_end_time + "')";

            if (dbhelper.ExecuteNoQuery(sqlstr) > 0)
                return true;
            else
                return false;
        }
        #endregion

        public System.Data.DataSet GetProblems(string condition)
        {
            string sqlstr = "select A.*,B.changeover_problems_type_name from Table_Cut_Problems as A,Table_Changeover_problems_type as B where A.cute_problem_type_id=B.changeover_problems_type_id and " + condition;
            return dbhelper.Query(sqlstr);
        }

        public System.Data.DataSet GetCutLog(string condition)
        {
            string sqlstr = "select A.cute_Log_ID,A.happened_time,B.changeover_status from Table_Cute_log as A,Table_Changeover_Status as B where A.changeover_status_id=B.changeover_status_id and " + condition;
            return dbhelper.Query(sqlstr);
        }

        public bool DeleteCutLog(string fieldValue)
        {
            string sqlstr = "delete Table_Cute_log where cute_Log_ID='" + fieldValue + "'";
            if (dbhelper.ExecuteNoQuery(sqlstr) > 0)
                return true;
            else
                return false;
        }
    }
}
